package com.bejson.pojo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.modules.st.pojo.PrintData;
import com.jeesite.modules.st.pojo.SourceMaterial;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyJson {

    public static void main(String[] args) throws IllegalAccessException, JsonProcessingException {
        JsonRootBean jsonRootBean = new JsonRootBean();
        jsonRootBean.template = "test.fr3";
        jsonRootBean.ver = 4;
        jsonRootBean.Copies = 1;//打印份数
        jsonRootBean.Duplex = 0;//是否双面
        jsonRootBean.Printer = "priPrinter";
//        PrintHeader printHeader=new PrintHeader();
//        printHeader.
//        SourceMaterial sourceMaterial = new SourceMaterial();
        List<PrintData> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            SourceMaterial sourceMaterial = new SourceMaterial();
            sourceMaterial.materialName = "原材料名称" + i;
            sourceMaterial.batchNumber = "批次号" + i;
            sourceMaterial.materialCode = "原料代码" + i;
            sourceMaterial.packType = "包装规格" + i;
            sourceMaterial.passDate = "2020-02-02" + i;
            sourceMaterial.barCode = "D:\\work\\java\\jeesite4\\root\\bar.png";
            list.add(sourceMaterial);
        }

        //设置table
        List<Col> colsList = genCols(SourceMaterial.class);
        List<Map<String, Object>> mapList = genMapList(list);

        Table table = new Table();
        table.Name = "ycl";
        table.Cols = colsList;
        table.Data = mapList;
        List<Table> tableList = new ArrayList<>();
        tableList.add(table);

        jsonRootBean.Tables=tableList;
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(jsonRootBean);
        System.out.println(json);

    }

    static List<Col> genCols(Class<? extends PrintData> clazz) {
        List<Col> list = new ArrayList<>();
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
//            declaredField.getType() //String
            Col col = new Col();
            col.type = "str";
            col.size = 255;
            col.name = declaredField.getName();
            list.add(col);
        }
        return list;
    }

    static List<Map<String, Object>> genMapList(List<PrintData> list) throws IllegalAccessException {
        List<Map<String, Object>> returnList = new ArrayList<>();
        for (PrintData printData : list) {
            Class<? extends PrintData> clazz = printData.getClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            Map<String, Object> map = new HashMap<>();
            for (Field declaredField : declaredFields) {
                map.put(declaredField.getName(), declaredField.get(printData));
            }
            returnList.add(map);
        }
        return returnList;
    }

}
